/*
 * Hidden Equity LLC
 * Author: Maksym Fedyay, Kyiv, mcsimf@gmail.com
 * Date: 29.02.2012
 */

package com.hiddenequity.report_definition_service;

import java.util.ArrayList;
import java.util.Map;

import com.hiddenequity.acts.R;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


public class ReportDialog {

	private static ArrayList<Map<String, String>> _rows = new ArrayList<Map<String,String>>();
	private static ArrayList<Map<String, String>> _columns = new ArrayList<Map<String,String>>();
	private static Activity _activity;
	private static int _fieldLimit;
	
	/*
	 * Create ccustom dialog for report displaying
	 */
	public static final Dialog showReport(final Activity activity, 
			ArrayList<Map<String, String>> rows, 
			ArrayList<Map<String, String>> columns,
			int fieldLimit,
			int title){
		
		_rows = rows;
		_columns = columns;
		_activity = activity;
		_fieldLimit = fieldLimit;
		
    	LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	
		View report = inflater.inflate(R.layout.report, null, false);
		ListView list_report = (ListView) report.findViewById(R.id.list_report);

        if(rows.size() != 0) {
        	list_report.setAdapter(reportAdapter);
        	list_report.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    				
					LinearLayout report_row_hidden = (LinearLayout) arg1.findViewById(R.id.report_row_hidden);
					ImageView report_expand_sign = (ImageView) arg1.findViewById(R.id.report_expand_sign);
					
					report_row_hidden.setVisibility(
    						report_row_hidden.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
			
    				final Drawable img_filter_off = activity.getResources().getDrawable( R.drawable.ico_toggle_off );
    				final Drawable img_filter_on = activity.getResources().getDrawable( R.drawable.ico_toggle_on );
			
    				report_expand_sign.setImageDrawable(
    						report_row_hidden.getVisibility() == View.VISIBLE ? img_filter_on : img_filter_off);
					
				}
			});
        	
//	    	for(Map<String, String> row : rows){
//	            
//	        	View view_report_row = inflater.inflate(R.layout.report_row, null, false);
//	        	TextView report_headline = (TextView) view_report_row.findViewById(R.id.report_headline);
//	            final ImageView report_expand_sign = (ImageView) view_report_row.findViewById(R.id.report_expand_sign);
//	        	
//	            report_expand_sign.setVisibility(fieldLimit > 0 ? View.VISIBLE : View.GONE);
//	            
//	        	LinearLayout report_row = (LinearLayout) view_report_row.findViewById(R.id.report_row);
//	            final LinearLayout report_row_hidden = (LinearLayout) view_report_row.findViewById(R.id.report_row_hidden);
//
//	            view_report_row.setClickable(true);
//
//	            view_report_row.setOnClickListener( new OnClickListener() {
//	    			public void onClick(View arg0) {
//	    				report_row_hidden.setVisibility(
//	    						report_row_hidden.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
//	    				
//	    				final Drawable img_filter_off = activity.getResources().getDrawable( R.drawable.ico_toggle_off );
//	    				final Drawable img_filter_on = activity.getResources().getDrawable( R.drawable.ico_toggle_on );
//	    				
//	    				report_expand_sign.setImageDrawable(
//	    						report_row_hidden.getVisibility() == View.VISIBLE ? img_filter_on : img_filter_off);
//	    			}
//	    		});
//	            
//	    		for(Map<String, String> column : columns){
//	    			for(Map.Entry<String, String> single_column : column.entrySet()){
//	    				
//	    				String field = single_column.getValue();
//	    				String value = row.get(field);
//	    				
//	    				if(columns.indexOf(column) == 0){
//	    					report_headline.setText(value);
//	    					continue;
//	    				}
//	    				
//		    			View report_field = inflater.inflate(R.layout.report_field, null, false);
//		            	TextView field_name = (TextView) report_field.findViewById(R.id.field_name);
//		            	TextView field_value = (TextView) report_field.findViewById(R.id.field_value);
//
//		            	field_name.setText(field);
//		            	field_value.setText(value);
//
//		            	if(columns.indexOf(column) + 1 > fieldLimit && fieldLimit > 0){
//		            		report_row_hidden.addView(report_field);
//		            	} else {
//			            	report_row.addView(report_field);
//		            	}
//	    			}
//	    		}
//	            ll_report.addView(view_report_row);
//	    	}
        } else {
//        	LayoutInflater li = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
//    		View v = li.inflate(R.layout.report_no_data, null, false);
//            ll_report.addView(v);
        }

		final Dialog dialog = new Dialog(activity);
        dialog.setContentView(report);
        dialog.setTitle(title);
        dialog.setCancelable(true);
		return dialog;
	}
	
	
	
	/*
     * Base adapter for listview
     */
	private static BaseAdapter reportAdapter = new BaseAdapter() {

		public int getCount() {
			int size = ReportDialog._rows.size();
			return size;
		}

		public Object getItem(int position) {
			return ReportDialog._rows.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;

				if (row == null) {
					LayoutInflater mInflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					row = mInflater.inflate(R.layout.report_row, null);
				}

				LinearLayout report_row = (LinearLayout) row.findViewById(R.id.report_row);
				LinearLayout report_row_hidden = (LinearLayout) row.findViewById(R.id.report_row_hidden);
				report_row.removeAllViews();
				report_row_hidden.removeAllViews();
				TextView report_headline = (TextView) row.findViewById(R.id.report_headline);
				
				LayoutInflater inflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				
				ImageView report_expand_sign = (ImageView) row.findViewById(R.id.report_expand_sign);
	        	
	            //report_expand_sign.setVisibility(View.GONE);
				report_expand_sign.setVisibility(_fieldLimit > 0 ? View.VISIBLE : View.GONE);
				
	    		for(Map<String, String> column : _columns){
	    			for(Map.Entry<String, String> single_column : column.entrySet()){
	    				
	    				String field = single_column.getValue();
	    				String value = ReportDialog._rows.get(position).get(field);
	    				
	    				if(_columns.indexOf(column) == 0){
	    					report_headline.setText(value);
	    					continue;
	    				}
	    				
		    			View report_field = inflater.inflate(R.layout.report_field, null, false);
		            	TextView field_name = (TextView) report_field.findViewById(R.id.field_name);
		            	TextView field_value = (TextView) report_field.findViewById(R.id.field_value);

		            	field_name.setText(field);
		            	field_value.setText(value);

		            	if(_columns.indexOf(column) + 1 > _fieldLimit && _fieldLimit > 0){
		            		report_row_hidden.addView(report_field);
		            	} else {
		            		report_row.addView(report_field);
		            	}
		            	
		            	//report_row.addView(report_field);
	    			}
	    		}

			return row;
		}
	};//campaignListAdapter
	
	
}
